﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using ST.Buz.Base.Enums;
using ST.Buz.Base.ViewModel;
using ST.Buz.Service;
using ST.Common.Util;
using ST.Common.Util.Model;
using ST.Common.Util.Mvc;
using ST.Common.Util.Text;
using System;
using System.Net;

namespace ST.Buz.Api.Controllers
{
	[Route("sys/role")]
	public class Sys_RoleController : BaseJsonController
	{
		private readonly IServiceSysRole service;
		public Sys_RoleController(IServiceSysRole _service,
			   ILogger<Sys_RoleController> _logger) : base(_logger)
		{
			service = _service;
		}

		#region 标准接口
		private VMS_Sys_Role GetSearchModelFromJson(JObject paras)
		{
			var vm = new VMS_Sys_Role();
			vm.pnum = TextHelper.GetInt(paras, "pnum");
			vm.psize = TextHelper.GetInt(paras, "psize");
			vm.sortorder = TextHelper.GetString(paras, "sortorder");

			vm.name = TextHelper.GetString(paras, "name");
			vm.code = TextHelper.GetString(paras, "code");
			vm.reserved = TextHelper.GetBoolNullable(paras, "reserved");
			vm.freezed = TextHelper.GetBoolNullable(paras, "freezed");
			vm.ids = TextHelper.GetInts(paras, "ids");
			return vm;
		}

		private VMS_Sys_Role GetSearchModelFromGetRequest()
		{
			var vm = new VMS_Sys_Role();
			vm.pnum = TextHelper.GetInt(Request.Query["pnum"]);
			vm.psize = TextHelper.GetInt(Request.Query["psize"]);
			vm.sortorder = TextHelper.GetString(Request.Query["sortorder"]);

			vm.name = TextHelper.GetString(Request.Query["name"]);
			vm.code = TextHelper.GetString(Request.Query["code"]);
			vm.reserved = TextHelper.GetBoolNullable(Request.Query["reserved"]);
			vm.freezed = TextHelper.GetBoolNullable(Request.Query["freezed"]);
			vm.ids = TextHelper.GetInts(Request.Query["ids"]);
			return vm;
		}

		private VMS_Sys_Role GetSearchModelFromPostRequest()
		{
			var vm = new VMS_Sys_Role();
			vm.pnum = TextHelper.GetInt(Request.Form["pnum"]);
			vm.psize = TextHelper.GetInt(Request.Form["psize"]);
			vm.sortorder = TextHelper.GetString(Request.Form["sortorder"]);

			vm.name = TextHelper.GetString(Request.Form["name"]);
			vm.code = TextHelper.GetString(Request.Form["code"]);
			vm.reserved = TextHelper.GetBoolNullable(Request.Form["reserved"]);
			vm.freezed = TextHelper.GetBoolNullable(Request.Form["freezed"]);
			vm.ids = TextHelper.GetInts(Request.Form["ids[]"]);
			return vm;
		}

		[HttpPost("search")]
		public IActionResult Search([FromBody]JObject paras)
		{
			try
			{
				//UserSession sessionCurrent = GetRawSessionFromRequest();
				var searchModel = GetSearchModelFromJson(paras);
				PageData pageData = service.GetPageList(searchModel);

				return new JsonResultPage(pageData);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, "请求出错", (int)HttpStatusCode.InternalServerError);
			}
		}
		#endregion

		#region 扩展接口
		#endregion
	}
}